//
//  HMChildrenShowCell.m
//  BestLearning
//
//  Created by 陈辉明 on 2017/12/11.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "HMChildrenShowCell.h"
#import "HNTeacgerEvaluateDetailModel.h"
#import "HNAudioTapGestureRecognizer.h"
#import "EMCDDeviceManager.h"
#import "HNAudioView.h"
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>


@interface HMChildrenShowCell()

@property (weak, nonatomic) IBOutlet UIImageView *oneIMageView;
@property (weak, nonatomic) IBOutlet UIImageView *twoImageView;
@property (weak, nonatomic) IBOutlet UIImageView *threeImageView;
@property (weak, nonatomic) IBOutlet UIButton *onePayButton;
@property (weak, nonatomic) IBOutlet UIButton *twoPayButton;


@property (weak, nonatomic) IBOutlet UIButton *threePayButton;
@property (nonatomic, strong) HNAudioView *AudioView;


@end

@implementation HMChildrenShowCell

-(void)setModel:(HNTeacgerEvaluateDetailModel *)model {
    
    _model = model;
    __block typeof(self) weakSelf = self;
    self.twoImageView.userInteractionEnabled = YES;
    self.oneIMageView.userInteractionEnabled = YES;
    self.threeImageView.userInteractionEnabled = YES;
    
    
    if (model.ImgF_Arr.count > 0) {
        
        if (model.ImgF_Arr.count == 1) {//有一张图片的情况
            self.oneIMageView.hidden = NO;
            self.oneIMageView.userInteractionEnabled = YES;
            BOOL IsPicture = [self JudgImgAndAudio:model.ImgF_Arr[0]];
            //判断是图片还是音频
            if (IsPicture) //是图片
            {
                HNAudioTapGestureRecognizer *Tap = [[HNAudioTapGestureRecognizer alloc] initWithTarget:self action:@selector(ImgAddTap:)];
                if ([NSString isBlankString:model.ImgF_Arr[0]]) {
                    
                }else {
                    [self.oneIMageView sd_setImageWithURL:[NSURL URLWithString:model.ImgF_Arr[0]] placeholderImage:[UIImage imageNamed:@"icon_add_default1"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                        Tap.Img = self.oneIMageView.image;
                    }];
                    [self.oneIMageView addGestureRecognizer:Tap];
                }
                
            }
            else // 是音频
            {
                self.oneIMageView.image = [UIImage imageNamed:@"iocn_voice"];
                if ([NSString isBlankString:model.ImgF_Arr[0]]) {
                    
                } else {
                    HNAudioTapGestureRecognizer *Tap = [[HNAudioTapGestureRecognizer alloc] initWithTarget:self action:@selector(PlayerAddTap:)];
                    Tap.UrlStr = model.ImgF_Arr[0];
                    [self.oneIMageView addGestureRecognizer:Tap];
                }
                
                
            }
            self.twoImageView.hidden = YES;
            self.threeImageView.hidden = YES;
            if (self.model.Videos_Arr.count > 0) {
                if (self.model.Videos_Arr.count == 1) {//有一个视频
                    self.twoImageView.hidden = NO;
                    self.onePayButton.hidden = YES;
                    self.twoPayButton.hidden = NO;
                    self.threePayButton.hidden = YES;

                    if ([NSString isBlankString:model.Videos_Arr[0][@"urlImg"]]) {
                        
                    } else {
                        if ([NSString isBlankString:model.Videos_Arr[0][@"url"]]) {
                            
                        }else {
                            [self.twoImageView sd_setImageWithURL:[NSURL URLWithString:model.Videos_Arr[0][@"urlImg"]] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                                HNAudioTapGestureRecognizer *Tap = [[HNAudioTapGestureRecognizer alloc] initWithTarget:self action:@selector(clickPlayVedioButton:)];
                                Tap.UrlStr = model.Videos_Arr[0][@"url"];
                                [weakSelf.twoImageView addGestureRecognizer:Tap];
                                
                                
                            }];
                        }
                        
                    }
                    
                } else {//有两个视频以上
                    self.twoImageView.hidden = NO;
                    self.onePayButton.hidden = YES;
                    self.twoPayButton.hidden = NO;
                    self.threePayButton.hidden = NO;
                    if ([NSString isBlankString:model.Videos_Arr[0][@"urlImg"]]) {
                        
                    }else {
                        if ([NSString isBlankString:model.Videos_Arr[0][@"url"]]) {
                            
                        }else {
                            [self.twoImageView sd_setImageWithURL:[NSURL URLWithString:model.Videos_Arr[0][@"urlImg"]] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                                HNAudioTapGestureRecognizer *Tap = [[HNAudioTapGestureRecognizer alloc] initWithTarget:self action:@selector(clickPlayVedioButton:)];
                                Tap.UrlStr = model.Videos_Arr[0][@"url"];
                                [weakSelf.twoImageView addGestureRecognizer:Tap];
                                
                                
                            }];
                        }
                        
                    }
                    self.threeImageView.hidden = NO;
                    if ([NSString isBlankString:model.Videos_Arr[1][@"urlImg"]]) {
                        
                    }else {
                        if ([NSString isBlankString:model.Videos_Arr[1][@"url"]]) {
                            
                        }else {
                            [self.threeImageView sd_setImageWithURL:[NSURL URLWithString:model.Videos_Arr[1][@"urlImg"]] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                                HNAudioTapGestureRecognizer *Tap = [[HNAudioTapGestureRecognizer alloc] initWithTarget:self action:@selector(clickPlayVedioButton:)];
                                Tap.UrlStr = model.Videos_Arr[1][@"url"];
                                [weakSelf.threeImageView addGestureRecognizer:Tap];
                            }];
                        }
                        
                    }
                    
                    
                }
                
            }
            
            
            
        } else if (model.ImgF_Arr.count == 2){//有两张图片的情况
            self.oneIMageView.hidden = NO;
            self.twoImageView.hidden = NO;
            self.threeImageView.hidden = YES;
            BOOL IsPicture = [self JudgImgAndAudio:model.ImgF_Arr[0]];
            //判断第一张图片是图片还是音频
            if (IsPicture) //是图片
            {
                
                if ([NSString isBlankString:model.ImgF_Arr[0]]) {
                    
                }else {
                    HNAudioTapGestureRecognizer *Tap = [[HNAudioTapGestureRecognizer alloc] initWithTarget:self action:@selector(ImgAddTap:)];
                    [self.oneIMageView sd_setImageWithURL:[NSURL URLWithString:model.ImgF_Arr[0]] placeholderImage:[UIImage imageNamed:@"icon_add_default1"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                        Tap.Img = self.oneIMageView.image;
                    }];
                    [self.oneIMageView addGestureRecognizer:Tap];
                }
                
            }
            else // 是音频
            {
                self.oneIMageView.image = [UIImage imageNamed:@"iocn_voice"];
                if ([NSString isBlankString:model.ImgF_Arr[0]]) {
                    
                }else {
                    HNAudioTapGestureRecognizer *Tap = [[HNAudioTapGestureRecognizer alloc] initWithTarget:self action:@selector(PlayerAddTap:)];
                    Tap.UrlStr = model.ImgF_Arr[0];
                    [self.oneIMageView addGestureRecognizer:Tap];
                }
                
                
            }
            BOOL IsPicture2 = [self JudgImgAndAudio:model.ImgF_Arr[1]];
            //判断第二张图片是图片还是音频
            if (IsPicture2) //是图片
            {
                if ([NSString isBlankString:model.ImgF_Arr[1]]) {
                    
                }else {
                    HNAudioTapGestureRecognizer *Tap = [[HNAudioTapGestureRecognizer alloc] initWithTarget:self action:@selector(ImgAddTap:)];
                    [self.twoImageView sd_setImageWithURL:[NSURL URLWithString:model.ImgF_Arr[1]] placeholderImage:[UIImage imageNamed:@"icon_add_default1"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                        Tap.Img = self.twoImageView.image;
                    }];
                    [self.twoImageView addGestureRecognizer:Tap];
                }
                
            }
            else // 是音频
            {
                self.twoImageView.image = [UIImage imageNamed:@"iocn_voice"];
                if ([NSString isBlankString:model.ImgF_Arr[1]]) {
                    
                }else {
                    HNAudioTapGestureRecognizer *Tap = [[HNAudioTapGestureRecognizer alloc] initWithTarget:self action:@selector(PlayerAddTap:)];
                    Tap.UrlStr = model.ImgF_Arr[1];
                    [self.twoImageView addGestureRecognizer:Tap];
                }
                
                
                
            }
            
            //有两张图片判断有没有视频
            if (self.model.Videos_Arr.count > 0) {
                self.threeImageView.hidden = NO;
                self.onePayButton.hidden = YES;
                self.twoPayButton.hidden = YES;
                self.threePayButton.hidden = NO;
                if ([NSString isBlankString:model.Videos_Arr[0][@"urlImg"]]) {
                    
                }else {
                    if ([NSString isBlankString:model.Videos_Arr[0][@"url"]]) {
                        
                    }else {
                        [self.threeImageView sd_setImageWithURL:[NSURL URLWithString:model.Videos_Arr[0][@"urlImg"]] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                            HNAudioTapGestureRecognizer *Tap = [[HNAudioTapGestureRecognizer alloc] initWithTarget:self action:@selector(clickPlayVedioButton:)];
                            Tap.UrlStr = model.Videos_Arr[0][@"url"];
                            [weakSelf.threeImageView addGestureRecognizer:Tap];
                            
                            
                        }];
                    }
                }
                
            } else {
                self.onePayButton.hidden = YES;
                self.twoPayButton.hidden = YES;
                self.threePayButton.hidden = YES;
            }
            
            
        } else if (model.ImgF_Arr.count == 3){ //有三张图片
            self.oneIMageView.hidden = NO;
            self.twoImageView.hidden = NO;
            self.threeImageView.hidden = NO;
            //判断第一张图片是图片还是音频
            BOOL IsPicture = [self JudgImgAndAudio:model.ImgF_Arr[0]];
            if (IsPicture) //是图片
            {
                if ([NSString isBlankString:model.ImgF_Arr[0]]) {
                    
                }else {
                    HNAudioTapGestureRecognizer *Tap = [[HNAudioTapGestureRecognizer alloc] initWithTarget:self action:@selector(ImgAddTap:)];
                    [self.oneIMageView sd_setImageWithURL:[NSURL URLWithString:model.ImgF_Arr[0]] placeholderImage:[UIImage imageNamed:@"icon_add_default1"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                        Tap.Img = self.oneIMageView.image;
                    }];
                    [self.oneIMageView addGestureRecognizer:Tap];
                }
                
            }
            else // 是音频
            {
                self.oneIMageView.image = [UIImage imageNamed:@"iocn_voice"];
                if ([NSString isBlankString:model.ImgF_Arr[0]]) {
                    
                }else {
                    HNAudioTapGestureRecognizer *Tap = [[HNAudioTapGestureRecognizer alloc] initWithTarget:self action:@selector(PlayerAddTap:)];
                    Tap.UrlStr = model.ImgF_Arr[0];
                    [self.oneIMageView addGestureRecognizer:Tap];
                }
                
            }
            BOOL IsPicture2 = [self JudgImgAndAudio:model.ImgF_Arr[1]];
            //判断第二张图片是图片还是音频
            if (IsPicture2) //是图片
            {
                if ([NSString isBlankString:model.ImgF_Arr[1]]) {
                    
                }else {
                    HNAudioTapGestureRecognizer *Tap = [[HNAudioTapGestureRecognizer alloc] initWithTarget:self action:@selector(ImgAddTap:)];
                    [self.twoImageView sd_setImageWithURL:[NSURL URLWithString:model.ImgF_Arr[1]] placeholderImage:[UIImage imageNamed:@"icon_add_default1"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                        Tap.Img = self.twoImageView.image;
                    }];
                    [self.twoImageView addGestureRecognizer:Tap];
                }
                
            }
            else // 是音频
            {
                self.twoImageView.image = [UIImage imageNamed:@"iocn_voice"];
                if ([NSString isBlankString:model.ImgF_Arr[1]]) {
                    
                } else {
                    HNAudioTapGestureRecognizer *Tap = [[HNAudioTapGestureRecognizer alloc] initWithTarget:self action:@selector(PlayerAddTap:)];
                    Tap.UrlStr = model.ImgF_Arr[1];
                    [self.twoImageView addGestureRecognizer:Tap];
                }
                
            }
            //判断有没有视频,没有显示三张都是图片,有显示2张图片一个视频
            //有两张图片判断有没有视频
            if (self.model.Videos_Arr.count > 0) {
                self.threeImageView.hidden = NO;
                self.onePayButton.hidden = YES;
                self.twoPayButton.hidden = YES;
                self.threePayButton.hidden = NO;
                if ([NSString isBlankString:model.Videos_Arr[0][@"urlImg"]]) {
                    
                }else {
                    if ([NSString isBlankString:model.Videos_Arr[0][@"url"]]) {
                        
                    }else {
                        [self.threeImageView sd_setImageWithURL:[NSURL URLWithString:model.Videos_Arr[0][@"urlImg"]] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                            HNAudioTapGestureRecognizer *Tap = [[HNAudioTapGestureRecognizer alloc] initWithTarget:self action:@selector(clickPlayVedioButton:)];
                            Tap.UrlStr = model.Videos_Arr[0][@"url"];
                            [weakSelf.threeImageView addGestureRecognizer:Tap];
                            
                            
                        }];
                    }
                }
                
            } else {
                self.onePayButton.hidden = YES;
                self.twoPayButton.hidden = YES;
                self.threePayButton.hidden = YES;
                
                BOOL IsPicture3 = [self JudgImgAndAudio:model.ImgF_Arr[2]];
                //判断第三张图片是图片还是音频
                if (IsPicture3) //是图片
                {
                    if ([NSString isBlankString:model.ImgF_Arr[2]]) {
                        
                    }else {
                        HNAudioTapGestureRecognizer *Tap = [[HNAudioTapGestureRecognizer alloc] initWithTarget:self action:@selector(ImgAddTap:)];
                        [self.threeImageView sd_setImageWithURL:[NSURL URLWithString:model.ImgF_Arr[2]] placeholderImage:[UIImage imageNamed:@"icon_add_default1"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                            Tap.Img = self.threeImageView.image;
                        }];
                        [self.threeImageView addGestureRecognizer:Tap];
                    }
                    
                }
                else // 是音频
                {
                    self.threeImageView.image = [UIImage imageNamed:@"iocn_voice"];
                    if ([NSString isBlankString:model.ImgF_Arr[2]]) {
                        
                    }else {
                        HNAudioTapGestureRecognizer *Tap = [[HNAudioTapGestureRecognizer alloc] initWithTarget:self action:@selector(PlayerAddTap:)];
                        Tap.UrlStr = model.ImgF_Arr[2];
                        [self.threeImageView addGestureRecognizer:Tap];
                    }
                    
                    
                }
                
            }
            
        }
    } //没有图片,判断有没有视频
    else {
        //判断有多少个视频
        
        if (self.model.Videos_Arr.count > 0) {
            if (self.model.Videos_Arr.count == 1) {
                self.twoImageView.hidden = YES;
                self.threeImageView.hidden = YES;
                self.oneIMageView.hidden = NO;
                self.onePayButton.hidden = NO;
                self.twoPayButton.hidden = YES;
                self.threePayButton.hidden = YES;
                if ([NSString isBlankString:model.Videos_Arr[0][@"urlImg"]]) {
                    
                }else {
                    if ([NSString isBlankString:model.Videos_Arr[0][@"url"]]) {
                        
                    }else {
                        [self.oneIMageView sd_setImageWithURL:[NSURL URLWithString:model.Videos_Arr[0][@"urlImg"]] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                            HNAudioTapGestureRecognizer *Tap = [[HNAudioTapGestureRecognizer alloc] initWithTarget:self action:@selector(clickPlayVedioButton:)];
                            Tap.UrlStr = model.Videos_Arr[0][@"url"];
                            [weakSelf.oneIMageView addGestureRecognizer:Tap];
                            
                            
                        }];
                    }
                }
                
            }
            if (self.model.Videos_Arr.count == 2) {
                self.oneIMageView.hidden = NO;
                self.twoImageView.hidden = NO;
                self.threeImageView.hidden = YES;
                self.onePayButton.hidden = NO;
                self.twoPayButton.hidden = NO;
                self.threePayButton.hidden = YES;
                if ([NSString isBlankString:model.Videos_Arr[0][@"urlImg"]]) {
                    
                }else {
                    if ([NSString isBlankString:model.Videos_Arr[0][@"url"]]) {
                        
                    }else {
                        [self.oneIMageView sd_setImageWithURL:[NSURL URLWithString:model.Videos_Arr[0][@"urlImg"]] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                            HNAudioTapGestureRecognizer *Tap = [[HNAudioTapGestureRecognizer alloc] initWithTarget:self action:@selector(clickPlayVedioButton:)];
                            Tap.UrlStr = model.Videos_Arr[0][@"url"];
                            [weakSelf.oneIMageView addGestureRecognizer:Tap];
                            
                            
                        }];
                    }
                    
                }
                if ([NSString isBlankString:model.Videos_Arr[1][@"urlImg"]]) {
                    
                }else {
                    if ([NSString isBlankString:model.Videos_Arr[1][@"url"]]) {
                        
                    }else {
                        [self.twoImageView sd_setImageWithURL:[NSURL URLWithString:model.Videos_Arr[1][@"urlImg"]] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                            HNAudioTapGestureRecognizer *Tap = [[HNAudioTapGestureRecognizer alloc] initWithTarget:self action:@selector(clickPlayVedioButton:)];
                            Tap.UrlStr = model.Videos_Arr[1][@"url"];
                            [weakSelf.twoImageView addGestureRecognizer:Tap];
                            
                            
                        }];
                    }
                }
                
            }
            if (self.model.Videos_Arr.count == 3) {
                self.oneIMageView.hidden = NO;
                self.twoImageView.hidden = NO;
                self.threeImageView.hidden = NO;
                self.onePayButton.hidden = NO;
                self.twoPayButton.hidden = NO;
                self.threePayButton.hidden = NO;
                if ([NSString isBlankString:model.Videos_Arr[0][@"urlImg"]]) {
                    
                }else {
                    if ([NSString isBlankString:model.Videos_Arr[0][@"url"]]) {
                        
                    }else {
                        [self.oneIMageView sd_setImageWithURL:[NSURL URLWithString:model.Videos_Arr[0][@"urlImg"]] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                            HNAudioTapGestureRecognizer *Tap = [[HNAudioTapGestureRecognizer alloc] initWithTarget:self action:@selector(clickPlayVedioButton:)];
                            Tap.UrlStr = model.Videos_Arr[0][@"url"];
                            [weakSelf.oneIMageView addGestureRecognizer:Tap];
                            
                            
                        }];
                    }
                }
                
                
                if ([NSString isBlankString:model.Videos_Arr[1][@"urlImg"]]) {
                    
                }else {
                    if ([NSString isBlankString:model.Videos_Arr[1][@"url"]]) {
                        
                    }else {
                        [self.twoImageView sd_setImageWithURL:[NSURL URLWithString:model.Videos_Arr[1][@"urlImg"]] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                            HNAudioTapGestureRecognizer *Tap = [[HNAudioTapGestureRecognizer alloc] initWithTarget:self action:@selector(clickPlayVedioButton:)];
                            Tap.UrlStr = model.Videos_Arr[1][@"url"];
                            [weakSelf.twoImageView addGestureRecognizer:Tap];
                            
                            
                        }];
                    }
                }
                
                
                if ([NSString isBlankString:model.Videos_Arr[2][@"urlImg"]]) {
                    
                }else {
                    if ([NSString isBlankString:model.Videos_Arr[2][@"url"]]) {
                        
                    }else {
                        [self.threeImageView sd_setImageWithURL:[NSURL URLWithString:model.Videos_Arr[2][@"urlImg"]] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                            HNAudioTapGestureRecognizer *Tap = [[HNAudioTapGestureRecognizer alloc] initWithTarget:self action:@selector(clickPlayVedioButton:)];
                            Tap.UrlStr = model.Videos_Arr[2][@"url"];
                            [weakSelf.threeImageView addGestureRecognizer:Tap];
                            
                            
                        }];
                    }
                }
                
            }
            
        }
    }
    
}


/** YES是图片,NO是音频 */
- (BOOL)JudgImgAndAudio:(NSString *)Str
{
    if ([Str isKindOfClass:[NSNull class]])
    {
        return YES;
    }
    else
    {
        if ([HNTeacgerEvaluateDetailModel JudgeStrIsImagOrAudio:Str])
        {
            return YES;
        }
        else
        {
            return NO;
        }
    }
}

#pragma mark - 图片全屏
/** 图片全屏手势 */
- (void)ImgAddTap:(HNAudioTapGestureRecognizer *)Tap
{
    [self ImgControllerWithImg:Tap.Img];
}
/** 图片全屏操作 */
- (void)ImgControllerWithImg:(UIImage *)Img
{
    [self.AudioView OpenWithImg:Img];
}


#pragma mark - 音频播放
/** 播放手势 */
- (void)PlayerAddTap:(HNAudioTapGestureRecognizer *)Tap
{
    [self PlayerWithUrlStr:Tap.UrlStr];
}
/** 音频播放 */
- (void)PlayerWithUrlStr:(NSString *)UrlStr
{
    [self RefreshDataForAmr:UrlStr Complete:^(NSString *FullPath) {
        
        FullPath = [FullPath substringFromIndex:7];
        [[EMCDDeviceManager sharedInstance] asyncPlayingWithPath:FullPath completion:^(NSError *error) {
            NSLog(@"播放完成");
        }];
    }];
}

/**
 获取amr文件
 */
- (void)RefreshDataForAmr:(NSString *)UrlStr Complete:(void(^)(NSString *FullPath))Complete
{
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject] stringByAppendingPathComponent:@"AMR"];
    
    [NetManager DownLoadFileWithUrlStr:UrlStr FileName:[UrlStr lastPathComponent] Path:path Progress:^(CGFloat Index) {
        
        
    } Complete:^(NSString *FullPath, NSError *Err) {
        
        [MBProgressHUD hideHUD];
        if (Err)
        {
            [MBProgressHUD showError:@"音频文件播放失败"];
        }
        else
        {
            Complete (FullPath);
        }
    }];
}

- (HNAudioView *)AudioView
{
    if (!_AudioView)
    {
        _AudioView = [[HNAudioView alloc] init];
    }
    return _AudioView;
}

//视频播放

- (void)clickPlayVedioButton:(HNAudioTapGestureRecognizer *)Tap {
    NSURL *URL = [NSURL URLWithString:Tap.UrlStr];
    MPMoviePlayerViewController  * moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:URL];
     [[self getCurrentVC] presentMoviePlayerViewControllerAnimated:moviePlayerController];
    moviePlayerController.moviePlayer.movieSourceType=MPMovieSourceTypeFile;
    
    
}


//获取当前控制器

- (UIViewController *)getCurrentVC {
    
    UIWindow *window = [[UIApplication sharedApplication].windows firstObject];
    if (!window) {
        return nil;
    }
    UIView *tempView;
    for (UIView *subview in window.subviews) {
        if ([[subview.classForCoder description] isEqualToString:@"UILayoutContainerView"]) {
            tempView = subview;
            break;
        }
    }
    if (!tempView) {
        tempView = [window.subviews lastObject];
    }
    
    id nextResponder = [tempView nextResponder];
    while (![nextResponder isKindOfClass:[UIViewController class]] || [nextResponder isKindOfClass:[UINavigationController class]] || [nextResponder isKindOfClass:[UITabBarController class]]) {
        tempView =  [tempView.subviews firstObject];
        
        if (!tempView) {
            return nil;
        }
        nextResponder = [tempView nextResponder];
    }
    return  (UIViewController *)nextResponder;
}


@end
